<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class AccountController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        parent::loadTemplate('magashop');
        $this->view->page = "blog";
	}
	
	public function indexAction(){
		$info = new Zenddn_System_DefaultInfo();
        
        $data['name'] = $info->getUserInfo('name');
        $data['email'] = $info->getUserInfo('email');
        $data['phone_number'] = $info->getUserInfo('phone_number');
        $data['address'] = $info->getUserInfo('address');
        
        $this->view->data = $data;
        $this->view->disableEmail = true;
        
        $request = $this->_request;
        
        if($request->isPost()){
            $validator = new Default_Form_ValidateAccount($request->getParams(), true, $data);
            
            if($validator->isError()){
                $this->view->messageError = $validator->getMessageError();
                $this->view->data = $validator->getData();
            }else{
                $data = $validator->getData();
                $data['id'] = $info->getUserInfo('id');
                
                $model = new Default_Model_Account();
                $model->saveData($data);
                
                $info = new Zenddn_System_DefaultInfo();
                $info->createInfo();
                
                $this->_helper->_redirector->gotoRoute(array(), 'account', true);
            }
        }
	}
    
    public function loginAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $type = $request->getParam('type');
            
            if($type == 'register'){
                $validator = new Default_Form_ValidateAccount($request->getParams());
                
                if($validator->isError()){
                    $this->view->messageError = $validator->getMessageError();
                    $this->view->data = $validator->getData();
                }else{
                    $data = $validator->getData();
                    $model = new Default_Model_Account();
                    $model->saveData($data);
                    
                    $activeCode = new Zenddn_Account_ActivationCode();
                    
                    $email = $data['email'];
                    $name = $data['name'];
                    $activationCode = $activeCode->generateActivationCode($email, $name);
                    
                    $mail = new Zenddn_Mail_Mail();
                    $mail->sendActivationCode($email, $name, $activationCode);
                    
                    $this->view->message = 'sent email to active account';
                    $this->render('success');
                }
            }else if($type = 'login'){
                $auth = new Zenddn_System_DefaultAuth();
                $result = $auth->login($request->getParams());
                
                if($result){
                    $info = new Zenddn_System_DefaultInfo();
                    
                    $info->createInfo();
                    
                    $this->_helper->_redirector->gotoRoute(array(), 'home', true);
                }else{
                    $this->view->error = true;
                }
            }
        }
    }
    
    public function logoutAction(){
    	$auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        
        $info = new Zenddn_System_DefaultInfo();
        $info->destroyInfo();
        
        $this->_helper->_redirector->gotoRoute(array(), 'home', true);
    	$this->_helper->viewRenderer->setNoRender();
    }
    
    public function changePasswordAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $info = new Zenddn_System_DefaultInfo();
            
            $id = $info->getUserInfo('id');
            
        	$model = new Default_Model_Account();
            $info = $model->getData($id);
            
            $validator = new Default_Form_ValidateChangePassword($request->getParams());
            $error = false;
            
            if($validator->isError()){
                $messageError = $validator->getMessageError();
                $error = true;
                
                $this->view->messageError = $messageError;
            }
            
            if(sha1($request->getParam('old_password')) != $info['password']){
                $messageError['old_password'] = "error old password";
                $error = true;
                
                $this->view->messageError = $messageError;
            }
            
            if(!$error){
                $data = $validator->getData();
                $data['id'] = $id;
                
                $model->saveData($data);
                
                $this->_helper->_redirector->gotoRoute(array(), 'account_logout', true);
            }
        }
    }
    
    public function forgotPasswordAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $email = $request->getParam('email');
            
            $model = new Default_Model_Account();
            
            if($model->checkEmailExists($email)){
                $info = $model->getDataByEmail($email);
                
                $generation = new Zenddn_Account_ActivationCode();
                $resetPasswordCode = $generation->generateResetPasswordCode($email, $info['name']);
                
                $mail = new Zenddn_Mail_Mail();
                $mail->sendResetPasswordCode($email, $info['name'], $resetPasswordCode);
                
                $this->view->message = 'sent email to reset password';
                $this->render('success');
            }else{
                $messageError['email'] = "invalid email";
                $this->view->messageError = $messageError;
            }
        }
    }
    
    public function recoverPasswordAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $email = $request->getParam('email');
            
            $model = new Default_Model_Account();
            
            $validator = new Default_Form_ValidateRecoverPassword($request->getParams());
            
            if($validator->isError()){
                $messageError = $validator->getMessageError();
                
                $this->view->messageError = $messageError;
            }else{
                $info = $model->getDataByEmail($email);
                
                $active = new Zenddn_Account_ActivationCode();
                echo $resetPasswordCode = $active->generateResetPasswordCode($email, $info['name']);
                
                if(strtolower($resetPasswordCode) == strtolower($request->getParam('resetcode'))){
                    $data = $validator->getData();
                    $info['password'] = $data['password'];
                    
                    $model->saveData($info);
                    
                    $this->view->message = 'change password success';
                    $this->render('success');
                }else{
                    $messageError['resetcode'] = 'invalid resetcode';
                    $this->view->messageError = $messageError;
                }
            }
        }
    }
    
    public function activeAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $validator = new Default_Form_ValidateActiveAccount($request->getParams());
            
            if($validator->isError()){
                $this->view->messageError = $validator->getMessageError();
                $this->view->data = $validator->getData();
            }else{
                $data = $validator->getData();
                $model = new Default_Model_Account();
                $rs = $model->active($data['email'], $data['activation_code']);
                
                if($rs){
                    $this->view->message = $rs['message_error'];
                    $this->render('success');
                }
            }
        }
    }
}